/**************************************************************************
** Qt Creator license header template
**   Special keywords: Programmer 2012/3/20 2012
**   Environment variables: 
**   To protect a percent sign, use '%'.
**************************************************************************/

#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    createActions();
    createMenus();
    createGlArea();
    createConnections();

    statusBar()->showMessage("Ready.");

    setWindowTitle(tr("My Fucking Thesis"));
    setMinimumSize(1024,768);
}

void MainWindow::createActions()
{
    //file menu's actions
    openAction = new QAction(tr("open"),this);
    closeAction = new QAction(tr("close"),this);
    clearAction = new QAction(tr("clear scene"),this);
    modelInfoAction = new QAction(tr("show model info"),this);

    //filter menu's actions
    quadricSimpAction = new QAction(tr("Quadric Simplification"),this);
    quadricPMAction = new QAction(tr("Quadric progressive mesh"),this);
    midPMAction = new QAction(tr("progressive mesh(midpoint)"),this);
    cleanMeshAction = new QAction(tr("Clean Mesh"),this);


    /////render menu's actions
    //render mode
    smoothModeAction = new QAction(tr("SMOOTH"),this);
    pointsModeAction = new QAction(tr("POINTS"),this);
    wireModeAction = new QAction(tr("WIRE"),this);
    flatWireModeAction = new QAction(tr("FLATWIRE"),this);
    hiddenModeAction = new QAction(tr("HIDDEN"),this);
    flatModeAction = new QAction(tr("FLAT"),this);
}

void MainWindow::createMenus()
{
    QMenu* fileMenu = menuBar()->addMenu(tr("File"));
    fileMenu->addAction(openAction);
    fileMenu->addAction(clearAction);
    fileMenu->addAction(modelInfoAction);
    fileMenu->addAction(closeAction);


    QMenu* filterMenu = menuBar()->addMenu(tr("Filter"));
    QMenu* simMenu = filterMenu->addMenu(tr("Mesh Simplification"));
    simMenu->addAction(quadricSimpAction);
    QMenu* pmMenu = filterMenu->addMenu(tr("Progressive mesh"));
    pmMenu->addAction(quadricPMAction);
    pmMenu->addAction(midPMAction);
    filterMenu->addAction(cleanMeshAction);

    QMenu* renderMenu = menuBar()->addMenu(tr("Render"));
    QMenu* renderModeMenu = renderMenu->addMenu(tr("Render Modes"));
    renderModeMenu->addAction(smoothModeAction);
    renderModeMenu->addAction(pointsModeAction);
    renderModeMenu->addAction(wireModeAction);
    renderModeMenu->addAction(flatWireModeAction);
    renderModeMenu->addAction(hiddenModeAction);
    renderModeMenu->addAction(flatModeAction);

}

void MainWindow::createConnections()
{
    connect(closeAction,SIGNAL(triggered()),this,SLOT(close()));
    connect(openAction,SIGNAL(triggered()),glDrawArea,SLOT(loadMesh()));
    connect(glDrawArea,SIGNAL(sendStatus(QString)),statusBar(),SLOT(showMessage(QString)));
    connect(clearAction,SIGNAL(triggered()),glDrawArea,SLOT(clear()));
    connect(modelInfoAction,SIGNAL(triggered()),glDrawArea,SLOT(showModelInfo()));

    //render mode connection
    connect(smoothModeAction,SIGNAL(triggered()),this,SLOT(setSoomthMode()));
    connect(pointsModeAction,SIGNAL(triggered()),this,SLOT(setPointsMode()));
    connect(wireModeAction,SIGNAL(triggered()),this,SLOT(setWireMode()));
    connect(flatWireModeAction,SIGNAL(triggered()),this,SLOT(setFlatWireMode()));
    connect(hiddenModeAction,SIGNAL(triggered()),this,SLOT(setHiddenMode()));
    connect(flatModeAction,SIGNAL(triggered()),this,SLOT(setFlatMode()));
    connect(this,SIGNAL(setRenderMode(int)),glDrawArea,SLOT(changeRenderMode(int)));

    //filter connections
    connect(quadricSimpAction,SIGNAL(triggered()),glDrawArea,SLOT(getQuadricParamsDialog()));
    connect(quadricPMAction,SIGNAL(triggered()),glDrawArea,SLOT(createProgressiveMesh()));
    connect(midPMAction,SIGNAL(triggered()),glDrawArea,SLOT(createMidpointPM()));
    connect(cleanMeshAction,SIGNAL(triggered()),glDrawArea,SLOT(cleanMesh()));
}

void MainWindow::createGlArea()
{
    glDrawArea = new GLArea(this);
    setCentralWidget(glDrawArea);
}



void MainWindow::setSoomthMode(){emit setRenderMode(0);}
void MainWindow::setPointsMode(){emit setRenderMode(1);}
void MainWindow::setWireMode(){emit setRenderMode(2);}
void MainWindow::setFlatWireMode(){emit setRenderMode(3);}
void MainWindow::setHiddenMode(){emit setRenderMode(4);}
void MainWindow::setFlatMode(){emit setRenderMode(5);}
